package com.garmin.tw.garminpcsync.android;

import java.net.URI;

import org.xmlrpc.android.XMLRPCClient;
import org.xmlrpc.android.XMLRPCMethod;
import org.xmlrpc.android.XMLRPCMethodCallback;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.garmin.tw.garminpcsync.android.adapter.TravelRouteAdapter;
import com.garmin.tw.garminpcsync.common.domain.xmlrpc.TravelRouteResult;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.Tracker;

public class TravelRouteListViewActivity extends ListActivity {

	private URI uri;
	
	private XMLRPCClient client;
	
	private ProgressDialog progressDialog;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Find the ListView resource.
		//mainListView = (ListView) findViewById(R.id.newsListView);
		
		this.uri = URI.create(getString(R.string.server_url));
		
		this.client = new XMLRPCClient(uri);
		
		XMLRPCMethod method = new XMLRPCMethod(client, "APIDispatcher.getTravelRouteList",
				new XMLRPCMethodCallback() {
					public void callFinished(Object result) {
						if (result instanceof TravelRouteResult) {
							TravelRouteResult trResult = (TravelRouteResult)result;
							
							if (trResult.getTravelRouteList() != null) {
								// Create ArrayAdapter using the planet list.
								//listAdapter = new ArrayAdapter<String>(TravelRouteListViewActivity.this, R.layout.simplerow, titleList);
								TravelRouteAdapter listAdapter = new TravelRouteAdapter(TravelRouteListViewActivity.this, trResult.getTravelRouteList());

								// Set the ArrayAdapter as the ListView's adapter.
								ListView mainListView = getListView();
								mainListView.setAdapter(listAdapter);
								mainListView.setOnItemClickListener(new OnItemClickListener() {
									@Override
									public void onItemClick(AdapterView<?> parent, View view,
											int position, long id) {
										// Get current context.
										Context mCtx = TravelRouteListViewActivity.this;
										// setContext will use mCtx to retrieve the application context.
										EasyTracker.getInstance().setContext(mCtx);
										// EasyTracker is now ready for use.
										Tracker myTracker = EasyTracker.getTracker();
										
										TextView title = (TextView) view.findViewById(R.id.description);
										
										myTracker.trackEvent("TravelRoute List Page", "Download TravelRoute", title.getText().toString(), null);
										
										// When clicked, show a toast with the TextView text
										Toast.makeText(getApplicationContext(), title.getText(), Toast.LENGTH_SHORT).show();
									}
								});
		
							}
						}
						
						progressDialog.cancel();
					}
				});
			
		progressDialog = new ProgressDialog(this);
		progressDialog.setMessage("Loading. Please wait...");
		progressDialog.show();
		
		Object[] params = { "3371570511", "zh_TW" };
		method.call(params);
	}
	
	@Override
	public void onStart() {
		super.onStart();
		// ... // The rest of your onStart() code.
		EasyTracker.getInstance().activityStart(this); // Add this method.
	}

	@Override
	public void onStop() {
		super.onStop();
		// ... // The rest of your onStop() code.
		EasyTracker.getInstance().activityStop(this); // Add this method.
	}
}